﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using QuanCafe.DataLayer;

namespace QuanCafe.admin
{
    public partial class Level : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.fnLoadData();
            }
        }
        /// <summary>
        /// load data
        /// </summary>
        private void fnLoadData()
        {
            try
            {
                QuanCafe.BusinessLayer.Level Level = new QuanCafe.BusinessLayer.Level();
                DataTable dtb = Level.GetAll();
                dtgLevel.DataSource = dtb;
                dtgLevel.DataBind();
                for (int i = 0; i < dtgLevel.Items.Count; i++)
                {
                    if (dtb.Rows[i]["Status"].ToString() == "1")
                    {
                        ((ImageButton)dtgLevel.Items[i].FindControl("btnActive")).ImageUrl = "~/Images/icons/btnOK.jpg";

                    }
                    else
                    {
                        ((ImageButton)dtgLevel.Items[i].FindControl("btnActive")).ImageUrl = "~/Images/icons/btnKO.jpg";
                    }
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
        public void fnClearControl()
        {
            txtLevelID.Text = "";
            txtLevelName.Text = "";
            txtLevelDesc.Text = "";
            lblError.Text = "";
        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            fnClearControl();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                QuanCafe.BusinessLayer.Level LevelObj = new QuanCafe.BusinessLayer.Level();
                if (txtLevelID.Text == "")
                {
                    LevelObj.LevelName = txtLevelName.Text.ToString();
                    if (LevelObj.CheckLevel() == true)
                    {
                        lblError.Text = "Tên chủ đề này đã tồn tại!Vui lòng nhập lại";
                    }
                    else
                    {
                        LevelObj.activity = "Insert";
                        LevelObj.LevelName = txtLevelName.Text.Trim();
                        LevelObj.LevelDesc = txtLevelDesc.Text.Trim();
                        LevelObj.Creator = Session["AdminName"].ToString();
                        LevelObj.InsertUpdate();
                        fnLoadData();
                        lblError.Text = "Thêm mới thành công";
                        fnClearControl();
                    }
                }
                else
                {
                    LevelObj.activity = "Update";
                    LevelObj.LevelID = Convert.ToInt32(txtLevelID.Text.Trim());
                    LevelObj.LevelName = txtLevelName.Text.Trim();
                    LevelObj.LevelDesc = txtLevelDesc.Text.Trim();
                    LevelObj.Creator = Session["AdminName"].ToString();
                    LevelObj.InsertUpdate();
                    fnLoadData();
                    lblError.Text = "Cập nhập thành công";
                    fnClearControl();
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                string strID = "";
                for (int i = 0; i < dtgLevel.Items.Count; i++)
                {
                    if (((CheckBox)dtgLevel.Items[i].FindControl("chkSelect")).Checked == true)
                    {
                        strID += dtgLevel.Items[i].Cells[0].Text.Trim() + "$";//Cot dau tien phai de cai ID                         
                    }
                }
                CLSCommon.DeleteListRecord("SP_Level", strID, "LevelID");
                fnLoadData();
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
        public void fnLoadDataByID(int IntID)
        {
            try
            {
                QuanCafe.BusinessLayer.Level Level = new QuanCafe.BusinessLayer.Level();
                Level.LevelID = IntID;
                DataRow dr = Level.GetByID();
                txtLevelID.Text = IntID.ToString();
                txtLevelName.Text = dr["LevelName"].ToString();
                txtLevelDesc.Text = dr["LevelDesc"].ToString();

            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }

        protected void dtgLevel_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string nName = e.CommandName.ToLower();
            int ID = -1;
            switch (nName)
            {
                case "edit":
                    ID = Convert.ToInt32(dtgLevel.Items[e.Item.ItemIndex].Cells[0].Text);
                    fnLoadDataByID(ID);
                    break;
                case "activechange":
                    ID = Convert.ToInt32(dtgLevel.Items[e.Item.ItemIndex].Cells[0].Text);
                    QuanCafe.BusinessLayer.Level Level = new QuanCafe.BusinessLayer.Level();
                    Level.LevelID = ID;
                    Level.ChangeActive();
                    break;
            }
            this.fnLoadData();
        }

        protected void dtgLevel_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            dtgLevel.CurrentPageIndex = e.NewPageIndex;
            this.fnLoadData();
        }
    }
}
